<template>
  <div class="search">
    <div>
      <div class="search_input">
        <div>
          <el-input :placeholder="placeholder" v-model="search" class="input">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchClick"
            ></el-button>
          </el-input>
        </div>
        <div class="hot_search one_txt_cut" v-if="searchHot.length != 0">
          热门搜索 : &nbsp;

          <span
            v-for="(item, index) in searchHot"
            :key="index"
            @click="hotSearchClick(item.first)"
            >{{ item.first }}
          </span>
        </div>
        <div style="display: flex; margin-top: 20px" v-else>
          <el-skeleton
            style="width: 70px; height: 20px; margin-right: 10px"
            :rows="1"
            animated
            v-for="(item, index) in 5"
            :key="index"
          >
            <el-skeleton-item variant="p" />
          </el-skeleton>
        </div>
      </div>
    </div>
    <div class="w">
      <el-tabs v-model="activeName" v-if="resultFlag">
        <el-tab-pane label="单曲" name="song">
          <SongsCpm :search="search" v-if="activeName == 'song'" />
        </el-tab-pane>
        <el-tab-pane label="专辑" name="album">
          <AlbumCpm :search="search" v-if="activeName == 'album'" />
        </el-tab-pane>
        <el-tab-pane label="歌手" name="singer">
          <SingerCpm :search="search" v-if="activeName == 'singer'" />
        </el-tab-pane>
        <el-tab-pane label="歌单" name="category">
          <CategoryCpm :search="search" v-if="activeName == 'category'" />
        </el-tab-pane>
        <el-tab-pane label="MV" name="mv">
          <MvCpm :search="search" v-if="activeName == 'mv'" />
        </el-tab-pane>
        <el-tab-pane label="用户" name="user">
          <UserCpm :search="search" v-if="activeName == 'user'" />
        </el-tab-pane>
      </el-tabs>
      <el-row
        v-else
        type="flex"
        justify="center"
        align="middle"
        style="padding-top: 20px; height: 500px"
      >
        <img
          src="../../assets/images/loading.3708220b.gif"
          alt=""
          height="200"
          width="200"
        />
      </el-row>
    </div>
  </div>
</template>

<script>
import { getSearchHot, getSearchDefault } from "../../api/searchApi";
import SongsCpm from "./components/songs.vue"; // 歌曲
import AlbumCpm from "./components/album.vue"; // 专辑
import SingerCpm from "./components/singer.vue"; // 歌手
import CategoryCpm from "./components/category.vue"; // 歌单
import MvCpm from "./components/mv.vue"; // mv
import UserCpm from "./components/user.vue"; // 用户
export default {
  name: "search",
  components: { SongsCpm, AlbumCpm, SingerCpm, CategoryCpm, MvCpm, UserCpm },
  data() {
    return {
      search: "",
      searchHot: [],
      placeholder: "",
      activeName: "song",
      resultFlag: false,
    };
  },
  created() {
    this.getSearchHotMthod();
    this.getSearchDefaultMethod();
  },
  watch: {},
  methods: {
    async getSearchHotMthod() {
      let result = await getSearchHot();
      if (result.code === 200) {
        this.searchHot = result.result.hots.slice(0, 5);
      }
    },
    async getSearchDefaultMethod() {
      let result = await getSearchDefault();
      if (result.code === 200) {
        this.placeholder = result.data.realkeyword;
      }
    },
    // 点击 热门搜索 的 span 的时候触发的函数
    hotSearchClick(content) {
      this.search = content;
      this.resultFlag = true;
    },
    searchClick() {
      if (this.search.length === 0) {
        this.search = this.placeholder;
      }

      this.resultFlag = true;
    },
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.search {
  // /deep/ .el-select-dropdown__list .selected {
  //   color: @globalColor;
  // }
  /deep/ .el-tabs__nav-scroll {
    padding-top: 20px;
    display: flex;
    justify-content: center;
  }
  /deep/ .el-tabs__item.is-active {
    color: @globalColor;
  }
  /deep/ .el-tabs .el-tabs__active-bar {
    background-color: @globalColor;
  }
  /deep/ .el-tabs .el-tabs__item {
    &:hover {
      color: @globalColor;
    }
  }
  /deep/ .el-tabs {
    padding-bottom: 20px;
  }

  /deep/ .el-pager li.active {
    color: #fff;
    background: @globalColor;
    border-radius: 50%;
    &:hover {
      color: #fff;
    }
  }
  /deep/ .el-pager li:hover {
    color: @globalColor;
  }

  /deep/ .is-first {
    width: 100% !important;
  }

  font-size: 14px;
  .search_input {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    height: 247px;
    background: url("../../assets/images/bg_search.webp") no-repeat center/cover;
    .input {
      width: 650px;
      height: 50px;
      /deep/ .el-input__inner {
        height: 50px;
        line-height: 50px;
      }
      /deep/ .el-icon-search {
        font-size: 18px;
        &:hover {
          color: @globalColor;
        }
      }
    }
    .hot_search {
      margin-top: 10px;
      color: #ffffff;
      font-size: 14px;
      span {
        cursor: pointer;
        margin-right: 10px;
        &:hover {
          color: @globalColor;
        }
      }
    }
  }
}
</style>
